package com.cl.code.common.utils;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextHolderStrategy;

/**
 * @author chengliang
 * @since 1.0.0
 */
public class CurrentUserUtils {

    public static final Long DEFAULT_USER_ID = 1L; // 默认用户ID，通常为系统管理员或测试用户

    /**
     * 获取当前用户ID
     *
     * @return 当前用户ID
     */
    public static Long getCurrentUserId() {
        // 在实际应用中，这里应该从安全上下文或会话中获取当前用户ID
        // 例如：return SecurityContextHolder.getContext().getAuthentication().getPrincipal().getId();
        // 这里为了示例，直接返回默认用户ID
        SecurityContextHolderStrategy contextHolderStrategy = SecurityContextHolder.getContextHolderStrategy();
        SecurityContext context = contextHolderStrategy.getContext();
        Authentication authentication = context.getAuthentication();
        if (authentication == null || !authentication.isAuthenticated()) {
            // 如果没有认证信息，返回默认用户ID
            return DEFAULT_USER_ID;
        }
        Object details = authentication.getDetails();
//        if (details instanceof UserInfo userInfo) {
//            // 如果认证信息包含用户信息，返回用户ID
//            return userInfo.getId();
//        }
        return DEFAULT_USER_ID;
    }

}
